﻿using System;
using System.IO;

namespace XTrace.Viewer
{
	sealed class SourceViewModel
	{
		public SourceViewModel(string fullPath)
		{
			_fullPath = fullPath;
			// XTrace.Warning("SourceViewModel", fullPath);
		}

		readonly string _fullPath;

		public string FullPath
		{
			get { return _fullPath; }
		}

		public string FileName
		{
			get { return Path.GetFileNameWithoutExtension(FullPath); }
		}

		DateTime _lastWriteTime;
		public DateTime LastWriteTime
		{
			get
			{
				UpdateInfo();
				return _lastWriteTime;
			}
		}

		bool _fileExists;
		public bool FileExists
		{
			get
			{
				UpdateInfo();
				return _fileExists;
			}
		}

		DateTime _lastUpdate;
		readonly TimeSpan _fromSeconds = TimeSpan.FromSeconds(4);

		void UpdateInfo()
		{
			var now = DateTime.UtcNow;
			if ((now - _lastUpdate) > _fromSeconds)
			{
				_fileExists = File.Exists(FullPath);
				if (_fileExists)
				{
					_lastWriteTime = File.GetLastWriteTimeUtc(FullPath);
				}
				_lastUpdate = now;
			}
		}
	}
}